<template>
  <div class="components-bind">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="nickName"
            :label="$t('distributionBind.customer')+':'"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionBind.customer')"
            />
          </el-form-item>
          <el-form-item
            prop="parentNickName"
            :label="$t('distributionProdLog.distributor')+':'"
          >
            <el-input
              v-model="searchForm.parentNickName"
              type="text"
              clearable
              :placeholder="$t('distributionProdLog.distributor')"
            />
          </el-form-item>
          <!-- 客户号码 -->
          <el-form-item
            prop="cUserMobile"
            :label="$t('distributionBind.userMobile')+':'"
          >
            <el-input
              v-model="searchForm.cUserMobile"
              type="text"
              clearable
              :placeholder="$t('distributionBind.userMobile')"
            />
          </el-form-item>
          <!-- 分销员号码 -->
          <el-form-item
            prop="dUserMobile"
            :label="$t('distributionProdLog.userMobile')+':'"
          >
            <el-input
              v-model="searchForm.dUserMobile"
              type="text"
              clearable
              :placeholder="$t('distributionProdLog.userMobile')"
            />
          </el-form-item>
          <el-form-item
            prop="state"
            :label="$t('distributionBind.currentRelationship')+':'"
          >
            <el-select
              v-model="searchForm.state"
              clearable
              :placeholder="$t('distributionBind.currentRelationship')"
            >
              <el-option
                :label="$t('seckill.loseEffectiveness')"
                value="-1"
              />
              <el-option
                :label="$t('distributionBind.binding')"
                value="1"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="resetForm('searchForm')"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          ref="bindTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
          @sort-change="sortChange"
        >
          <el-table-column
            :label="$t('number')"
            type="index"
            width="80"
          />
          <el-table-column
            :label="$t('distributionBind.customer')"
            prop="nickName"
          >
            <template #default="scope">
              {{ scope.row.user.nickName || '-' }}
            </template>
          </el-table-column>
          <!-- 客户号码 -->
          <el-table-column
            :label="$t('distributionBind.userMobile')"
            prop="nickName"
          >
            <template #default="scope">
              {{ scope.row.user.userMobile || '-' }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionProdLog.distributor')"
            prop="parentNickName"
          >
            <template #default="scope">
              {{ scope.row.distributionUser.nickName || '-' }}
            </template>
          </el-table-column>
          <!-- 分销员号码 -->
          <el-table-column
            :label="$t('distributionProdLog.userMobile')"
            prop="parentNickName"
          >
            <template #default="scope">
              {{ scope.row.distributionUser.userMobile || '-' }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distribution.bindingTime')"
            prop="bindTime"
            sortable
          />
          <el-table-column
            :label="$t('distributionBind.failureTime')"
            prop="invalidTime"
            sortable
          >
            <template #default="scope">
              <span v-if="scope.row.invalidTime === null">{{ $t('distribUserWallet.nothing') }}</span>
              <span v-else>{{ scope.row.invalidTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionBind.currentRelationship')"
            align="center"
            prop="state"
          >
            <template #default="scope">
              <span v-if="scope.row.state === -1">{{ $t('seckill.loseEffectiveness') }}</span>
              <span v-if="scope.row.state === 1">{{ $t('distributionBind.binding') }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
let tempSearchForm = null // 保存上次点击查询的请求条件
let theParams = null // 保存上次点击查询的请求条件

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  nickName: '',
  parentNickName: '',
  state: ''
})
const dataList = ref([])
let sortParam = 0 // 排序字段 0无 1.绑定时间 2.失效时间
let sortType = 0 // 0无 1 正序 2倒序
onMounted(() => {
  getDataList()
})

// 点击查询
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
// 获取数据列表
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    theParams = JSON.parse(JSON.stringify(searchForm))
    tempSearchForm = {
      current: pageParam ? pageParam.currentPage : 1,
      size: pageParam ? pageParam.pageSize : 10,
      sortParam,
      sortType
    }
  } else {
    tempSearchForm.current = page ? page.currentPage : 1
    tempSearchForm.size = page ? page.pageSize : 10
  }
  http({
    url: http.adornUrl('/distribution/distributionUserBind/page'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, theParams))
  }).then(({ data }) => {
    page.total = data.total
    page.pageSize = data.size
    page.currentPage = data.current
    dataList.value = data.records
  })
}
// 对应6种排序
const sortChange = (data) => {
  // 排序字段 0无 1.绑定时间 2.失效时间
  switch (data.prop) {
    case 'bindTime': sortParam = 1
      break
    case 'invalidTime': sortParam = 2
      break
    default: sortParam = 0
      break
  }
  // 排序类型 0无 1 正序 2倒序
  switch (data.order) {
    case 'descending': sortType = 2
      break
    case 'ascending': sortType = 1
      break
    default: sortType = 0
      break
  }
  getDataList(page, true)
}
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

// 重置搜索表单
const searchFormRef = ref(null)
const resetForm = () => {
  searchFormRef.value.resetFields()
}
</script>
